﻿package bdz.common.utils{
	import com.codeazur.as3swf.SWF;
	import com.codeazur.as3swf.SWFData;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import flash.events.*;
	/**
	 * ...
	 * @author Donald
	 */
	public class SWFParser extends EventDispatcher {
		
		private var _stream:URLStream;
		private var _bytes:ByteArray;
		private var _reader:SWF;
		private var _dynamicFields:Array;
		private var _parsed:Boolean = false;
		function SWFParser(src:String):void {
			createStream(src);
		}
		private function createStream(src:String):void {
			_stream = new URLStream();
			_bytes = new ByteArray();
			_stream.load(new URLRequest(src));
			_stream.addEventListener(ProgressEvent.PROGRESS, onStreamProgress);
			_stream.addEventListener(Event.COMPLETE, onStreamComplete);
		}
		private function startParsing():void {
			dispatchEvent(new Event(Event.COMPLETE, false, false));
			_reader = new SWF(_bytes);
			_dynamicFields = new Array();
			var tags:Array = _reader.tags;
			for each(var obj:* in _reader.tags) {
				
			}
		}
		private function onStreamComplete(evt:Event):void {
			_stream.close();
			startParsing();
		}
		private function onStreamProgress(evt:ProgressEvent):void {
			if (_stream.bytesAvailable == 0) return;
			if (_stream.connected) _stream.readBytes(_bytes, _bytes.length);
		}
		public function publish():void {
			
		}
		public function get dynamicFields():Array {
			return _dynamicFields;
		}
		public function get data():SWFData {
			if (_parsed) {
				var sdata:SWFData = new SWFData();
				_reader.publish(sdata);
				return sdata;
			}
			return null;
		}
		
	}
	
}